package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import data.dto.userTypeDTO;

import util.CurrentUser;

import manager.UserManager;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login/*")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("title", "Login Page");
		String page = "";
		String doAction = request.getParameter("do") == null ? "" : request
				.getParameter("do");

		HttpSession session = request.getSession();
		
		if (doAction.equalsIgnoreCase("logout")) {
			session.removeAttribute("user");
			session.removeAttribute("ismanager");
			page = "/staff/login.jsp";
			request.setAttribute("user", "Staff");
		} else {
			if (util.CurrentUser.getCurrentUser(session) != null) {
				// redirect to another page
				UserManager userManager = new UserManager();
				page = userManager.getDefaultPage(session);

			} else {
				// user login checking and process
				String username = request.getParameter("username") == null ? ""
						: request.getParameter("username");
				String password = request.getParameter("password") == null ? ""
						: request.getParameter("password");

				boolean isAdmin = (request.getPathInfo() != null && request
						.getPathInfo().toLowerCase().indexOf("admin") > -1) ? true
						: false;

				if (username.length() > 0 || password.length() > 0) {
					boolean isEmpty = false;
					if (request.getParameter("username").length() == 0) {
						request.setAttribute("usernamemessage",
								"you need to fill username");
						isEmpty = true;
					}

					if (request.getParameter("password").length() == 0) {
						request.setAttribute("passwordmessage",
								"you need to fill password");
						isEmpty = true;
					}

					// user login
					if (!isEmpty) {

						UserManager userManager = new UserManager();

						isAdmin = request.getParameter("admin") != null ? true
								: false;
						
						session = request.getSession(true);

						if (userManager.login(username, password, isAdmin, session)) {
							// redirect to another page
							page = userManager.getDefaultPage(session);
						} else {
							request.setAttribute("username",
									request.getParameter("username"));
							request.setAttribute("message",
									"Login failed. Please check your username and password");
						}
					} else if (request.getParameter("username").length() > 0) {
						request.setAttribute("username",
								request.getParameter("username"));
					}
				} else if (request.getParameter("login") != null) {
					request.setAttribute("usernamemessage",
							"you need to fill username");
					request.setAttribute("passwordmessage",
							"you need to fill password");
				}

				if (page == "" || page == "/") {
					if (isAdmin) {
						page = "/admin/login.jsp";
						request.setAttribute("user", "Admin");
					} else {
						page = "/staff/login.jsp";
						request.setAttribute("user", "Staff");
					}
				}
			}
		}

		RequestDispatcher rd = getServletContext().getRequestDispatcher(page);
		rd.forward(request, response);
	}
}
